package com.ai.networking.netty.serverDynamicsLink;


import com.ai.networking.netty.service.NettyServer;

import java.util.Observable;
import java.util.Observer;

/**
 * 动态链路主题
 * @Author jiaok
 * @Date 2024-05-13 15:32
 */
public class LinkObserver implements Observer {

    /**
     * 监听的主题
     */
    Observable observable;

    /**
     * 链路ip
     */
    private String linkIp;

    /**
     * 链路port
     */
    private Integer linkPort;


    public LinkObserver(Observable observable) {
        this.observable = observable;
        /**
         * 将当前观察者登记到主题中
         */
        observable.addObserver(this);
    }


    @Override
    public void update(java.util.Observable o, Object arg) {
        if (o instanceof LinkDynamicsTheme){
            LinkDynamicsTheme linkDynamicsTheme = (LinkDynamicsTheme) o;
            this.linkIp=linkDynamicsTheme.getLinkIp();
            this.linkPort=linkDynamicsTheme.getLinkPort();
            //创建链路
            createServerLink();
        }
    }

    public void createServerLink(){
        int[] ports = new int[1];
        ports[0]=this.linkPort;
        AsyncQueue.submit(new NettyServer(ports));
    }

}
